package com.hzit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.hzit.dto.ClassesDTO;
import com.hzit.entity.Classes;
import com.hzit.mapper.ClassesMapper;
import com.hzit.service.IClassesService;
import com.hzit.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

public class ClassesServiceImpl implements IClassesService {

	//1. 查询所有的班级
	@Override
	public List<ClassesDTO> findAll() throws IOException {
		//1.1 得到SqlsessionFactory
		SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
		//1.2 开始查询
		try(SqlSession session = sqlSessionFactory.openSession()){
			ClassesMapper mapper = session.getMapper(ClassesMapper.class);
			List<Classes> classes = mapper.findAll();
			//1.3 将List<Classes>转换为List<ClassesDTO>集合
			return classes.stream().map(m -> {
				ClassesDTO dto = new ClassesDTO();
				BeanUtil.copyProperties(m, dto, true);
				return dto;
			}).collect(Collectors.toList());
		}

	}
}
